package fei.yuzhi.com.loveflowers.fragment.mefragment.login.forgetPass;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.base.BaseActivity;
import fei.yuzhi.com.loveflowers.fragment.mefragment.login.register.RegisterActivity;

public class ForgetPassActivity extends BaseActivity implements ForgetPassContract.ViewForgetPass {

    @BindView(R.id.iv_forgetPass_back)
    ImageView ivForgetPassBack;
    @BindView(R.id.et_forgetPass_userTell)
    EditText etForgetPassUserTell;
    @BindView(R.id.et_forgetPass_userDuanXin)
    EditText etForgetPassUserDuanXin;
    @BindView(R.id.iv_forgetPass_userDuanXin)
    ImageView ivForgetPassUserDuanXin;
    @BindView(R.id.btn_forgetPass_userYes)
    Button btnForgetPassUserYes;
    @BindView(R.id.et_forgetPass_againPass)
    EditText etForgetPassAgainPass;
    @BindView(R.id.et_forgetPass_userName)
    EditText etForgetPassUserName;

    private ForgetPassPresenter presenter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        presenter = new ForgetPassPresenter(this);

    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_forgetpass;
    }


    @Override
    public void onVerticalSucess(String msg) {
        if ("修改成功".equals(msg)){
            Toast.makeText(ForgetPassActivity.this, msg+",请登录", Toast.LENGTH_SHORT).show();
            finish();
        }else {
            Toast.makeText(ForgetPassActivity.this, msg, Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void sendSituation(String msg) {
        Toast.makeText(ForgetPassActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @OnClick({R.id.iv_forgetPass_back, R.id.iv_forgetPass_userDuanXin, R.id.btn_forgetPass_userYes})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_forgetPass_back:
                finish();
                break;
            case R.id.iv_forgetPass_userDuanXin:
                sendDuanXin();
                break;
            case R.id.btn_forgetPass_userYes:
                changePass();
                break;
        }
    }

    private void sendDuanXin() {
        String path = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        String tell = etForgetPassUserTell.getText().toString().trim();
        Pattern pattern = null;
        boolean flag = pattern.compile(path).matcher(tell).matches();
        //手机号判断
        if (flag) {
            presenter.sendYanZhengMa(ForgetPassActivity.this, tell);
        } else {
            Toast.makeText(ForgetPassActivity.this, "手机号不正确", Toast.LENGTH_SHORT).show();
        }
    }

    private void changePass() {
        String name = etForgetPassUserName.getText().toString().trim();
        String tell = etForgetPassUserTell.getText().toString().trim();
        String newPass = etForgetPassAgainPass.getText().toString();

        String path = "^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        Pattern pattern = null;
        boolean flag = pattern.compile(path).matcher(tell).matches();
        //判空
        if (RegisterActivity.isNull(etForgetPassUserName) && RegisterActivity.isNull(etForgetPassAgainPass)) {
            //手机号验证
            if (flag) {

                //验证验证码
//                        BmobSMS.verifySmsCode(ForgetPassActivity.this,tell,etForgetPassUserDuanXin.getText().toString().trim(), new VerifySMSCodeListener() {
//                            @Override
//                            public void done(cn.bmob.sms.exception.BmobException e) {
//                                // TODO Auto-generated method stub
//                                if(e==null){//短信验证码已验证成功
//                                    Log.i("bmob", "验证通过");

                                    presenter.userPass(ForgetPassActivity.this,name,newPass,tell);
//                                }else{
//                                    Toast.makeText(ForgetPassActivity.this, "短信验证码验证错误", Toast.LENGTH_SHORT).show();
//                                    Log.i("bmob", "验证失败：code ="+e.getErrorCode()+",msg = "+e.getLocalizedMessage());
//                                }
//                            }
//                        });


            } else {
                Toast.makeText(ForgetPassActivity.this, "手机号不正确", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(ForgetPassActivity.this, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
        }
    }
}
